Skip to content

gh-153569: return token spans and unify decoded source storage - #156482

Open
pablogsal wants to merge 6 commits into
python:mainfrom
pablogsal:gh-153569-tokenizer-offset-state
Open

gh-153569: return token spans and unify decoded source storage#156482
pablogsal wants to merge 6 commits into
python:mainfrom
pablogsal:gh-153569-tokenizer-offset-state

Conversation

@pablogsal

@pablogsal pablogsal commented Aug 27, 2026

Copy link
Copy Markdown
Member

Return tokenizer tokens as logical source spans with start/end locations, and give all decoded input one owner in SourceText.

File and readline input discard consumed windows while advancing a logical base offset. Discard reuses the allocation and resets line metadata; prepared and interactive input retain their source. The separate streaming buffer allocation is removed. This prerequisite still rebases scanner pointers around append; #156654 replaces those pointers with offsets.

Debug source growth forces relocation. Tests cover f-/t-string growth in both token modes, interactive multiline input, discard/reuse, implicit-line metadata reset, and offset limits.

This follows #156472. The rest of the stack is #156484, then #156654.

Validation on current main: the full debug suite ran 52,011 tests. Only perf-profiler tests failed during simultaneous runs with shared perf mapping-resource errors; all 14 profiler tests passed in the sequential rerun. Patchcheck and Windows project/source checks passed.

@pablogsal

Copy link
Copy Markdown
Member Author

Do not review until #156472 lands.

@pablogsal
pablogsal force-pushed the gh-153569-tokenizer-offset-state branch from dddc133 to 1753605 Compare August 28, 2026 15:32
@pablogsal

Copy link
Copy Markdown
Member Author

#156472 has landed; this is rebased on it now.

@pablogsal
pablogsal force-pushed the gh-153569-tokenizer-offset-state branch from 1753605 to 8bd1d3a Compare August 28, 2026 16:24
@pablogsal pablogsal changed the title gh-153569: move tokenizer state to source offsets gh-153569: drive tokenizer input through source offsets Aug 28, 2026
@pablogsal
pablogsal force-pushed the gh-153569-tokenizer-offset-state branch from 8bd1d3a to c10fd82 Compare August 28, 2026 18:08
@pablogsal pablogsal changed the title gh-153569: drive tokenizer input through source offsets gh-153569: return tokenizer tokens as source spans Aug 28, 2026

@linakhan470-cell linakhan470-cell left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

183

@pablogsal
pablogsal marked this pull request as ready for review August 28, 2026 21:15
@pablogsal
pablogsal requested a review from lysnikolaou as a code owner August 28, 2026 21:15
@pablogsal
pablogsal force-pushed the gh-153569-tokenizer-offset-state branch 3 times, most recently from 150ee6a to f134e81 Compare August 30, 2026 16:10
@pablogsal pablogsal changed the title gh-153569: return tokenizer tokens as source spans gh-153569: return token spans and unify decoded source storage Sep 5, 2026
File and readline tokenizers keep every byte they read because the lexer owns
buffer growth and repairs its pointers after reallocations. Long inputs
therefore grow tokenizer memory with the entire source.

Let the reader reuse a bounded input window when no token or formatted string
needs older bytes. Track the absolute offset of that window and save pointer
offsets only when backing storage moves.
Tokenizer results expose pointers into the active input buffer. That ties every
consumer to the buffer lifetime and prevents the reader from reusing older
storage.

Return logical source spans with their start and end locations instead. Pegen
and `_tokenize` materialize a short-lived view before requesting another token,
and the tokenizer no longer keeps a second end pointer for the last token.
@pablogsal
pablogsal force-pushed the gh-153569-tokenizer-offset-state branch from bf25f7b to 6117ae3 Compare September 5, 2026 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants